"""
Case Type   : SQL语法
Case Name   : alter修改索引表空间后,表定义索引处显示修改后的tablespace
Create At   : 2023/02/15
Owner       : cr13
Description :
    1.创建用户及表空间
    2.创建表,插入数据
    3.为表创建索引
    4.查询表结构
    5.修改索引表空间
    6.再次查询表结构
    7.清理环境
Expect      :
    1.成功
    2.成功
    3.成功
    4.成功
    5.成功
    6.成功
    7.成功
History     :
"""

import os
import unittest
from yat.test import macro
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class Tools(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.constant = Constant()
        self.commsh = CommonSH('PrimaryDbUser')
        self.uname = 'u_ddl_index_0260'
        self.tablespace = 'ta_ddl_index_0260'
        self.tbname1 = 't_ddl_index_0260_01'
        self.tbname2 = 't_ddl_index_0260_02'
        self.index = 'i_ddl_index_0260'

    def test_gs_upgradectl(self):
        text = "----step1:创建用户及表空间 expect:成功----"
        self.log.info(text)
        sql_cmd = f"drop user if exists {self.uname};" \
            f"create user {self.uname} password '{macro.COMMON_PASSWD}';"
        result = self.commsh.execut_db_sql(sql_cmd)
        self.log.info(result)
        self.assertIn(self.constant.DROP_ROLE_SUCCESS_MSG, result, '执行失败' +
                      text)
        self.assertIn(self.constant.CREATE_ROLE_SUCCESS_MSG, result, '执行失败'
                      + text)
        sql_cmd = f"drop tablespace if exists {self.tablespace};" \
            f"create tablespace {self.tablespace} owner {self.uname} " \
            f"relative location 'my_location';;"
        result = self.commsh.execut_db_sql(sql_cmd)
        self.log.info(result)
        self.assertIn(self.constant.TABLESPCE_DROP_SUCCESS, result, '执行失败'
                      + text)
        self.assertIn(self.constant.TABLESPCE_CREATE_SUCCESS, result,
                      '执行失败' + text)

        text = "----step2:创建表,插入数据 expect:成功----"
        self.log.info(text)
        sql_cmd = f"drop table if exists {self.tbname1};" \
            f"create table {self.tbname1}(c1 int,score number,primary " \
            f"key(c1));" \
            f"insert into {self.tbname1} select generate_series(1,100);"
        result = self.commsh.execut_db_sql(sql_cmd)
        self.log.info(result)
        self.assertIn(self.constant.TABLE_DROP_SUCCESS, result, '执行失败' +
                      text)
        self.assertIn(self.constant.CREATE_TABLE_SUCCESS, result, '执行失败'
                      + text)
        self.assertIn(self.constant.INSERT_SUCCESS_MSG, result, '执行失败' +
                      text)
        sql_cmd = f"drop table if exists {self.tbname2};" \
            f"create table {self.tbname2}(c1 int,c2 varchar2 default " \
            f"'xiaomimg',c3 number,c4 money,c5 char(20),c6 clob,c7 blob," \
            f"c8 date,c9 boolean,c10 timestamp,c11 point,columns12 cidr," \
            f"primary key(c1),foreign key(c1) references {self.tbname1}(c1)," \
            f"check(c3>0),unique(c1)) with(segment=on,fillfactor=70," \
            f"orientation=row) tablespace {self.tablespace};"
        result = self.commsh.execut_db_sql(sql_cmd)
        self.log.info(result)
        self.assertIn(self.constant.TABLE_DROP_SUCCESS, result, '执行失败' +
                      text)
        self.assertIn(self.constant.CREATE_TABLE_SUCCESS, result, '执行失败'
                      + text)

        text = "----step3:为表创建索引 expect:成功----"
        self.log.info(text)
        sql_cmd = f"drop index if exists {self.index};" \
            f"create index {self.index} on {self.tbname2}(c7);"
        result = self.commsh.execut_db_sql(sql_cmd)
        self.log.info(result)
        self.assertIn(self.constant.DROP_INDEX_SUCCESS_MSG, result, '执行失败'
                      + text)
        self.assertIn(self.constant.CREATE_INDEX_SUCCESS_MSG, result,
                      '执行失败' + text)

        text = "----step4:查询表结构 expect:成功----"
        self.log.info(text)
        sql_cmd = f"\\\d+ {self.tbname2}"
        result = self.commsh.execut_db_sql(sql_cmd)
        self.log.info(result)
        str1 = f'"{self.index}" btree (c7) TABLESPACE pg_default'
        self.assertIn(str1, result, '执行失败' + text)

        text = "----step5:修改索引表空间 expect:成功----"
        self.log.info(text)
        sql_cmd = f"alter index {self.index} set tablespace {self.tablespace};"
        result = self.commsh.execut_db_sql(sql_cmd)
        self.log.info(result)
        self.assertIn(self.constant.ALTER_INDEX_SUCCESS_MSG, result, '执行失败'
                      + text)

        text = "----step6:再次查询表结构 expect:成功----"
        self.log.info(text)
        sql_cmd = f"\\\d+ {self.tbname2}"
        result = self.commsh.execut_db_sql(sql_cmd)
        self.log.info(result)
        str1 = f'"{self.index}" btree (c7) TABLESPACE {self.tablespace}'
        self.assertIn(str1, result, '执行失败' + text)

    def tearDown(self):
        text = '-----step7:清理环境 expect:成功-----'
        self.log.info(text)
        sql_cmd = f"drop table if exists {self.tbname2} cascade;" \
            f"drop table if exists {self.tbname1} cascade;" \
            f"drop tablespace if exists {self.tablespace};" \
            f"drop user if exists {self.uname};"
        result = self.commsh.execut_db_sql(sql_cmd)
        self.log.info(result)
        self.assertIn(self.constant.TABLESPCE_DROP_SUCCESS, result, '执行失败'
                      + text)
        self.assertIn(self.constant.DROP_ROLE_SUCCESS_MSG, result, '执行失败' +
                      text)
        self.assertEqual(result.count(self.constant.TABLE_DROP_SUCCESS), 3,
                         '执行失败' + text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
